/*
 * 		Test.java
 *   Copyright (C) 2015  Adrián E. Córdoba [software.asia@gmail.com]
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ar.com.adriancordoba.app.console.atps.manager;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import ar.com.adriancordoba.app.console.atps.configurations.Configuration;
import ar.com.adriancordoba.app.console.atps.configurations.Logging;
import ar.com.adriancordoba.app.console.atps.exceptions.AutomaticTasksProcessingSystemException;
import ar.com.adriancordoba.app.console.atps.manager.state.Initialization;
import ar.com.adriancordoba.app.console.atps.manager.state.Processing;
import ar.com.adriancordoba.app.console.atps.manager.state.Recovery;
import ar.com.adriancordoba.app.console.atps.manager.state.Starting;
import ar.com.adriancordoba.app.console.atps.manager.state.State;
import ar.com.adriancordoba.app.console.atps.manager.state.Stopping;

/**
 *
 * @author Adrián E. Córdoba
 */
public class AppManager {
	private State initialization;
	private State starting;
	private State recovery;
	private State processing;
	private State stopping;
	private State state;

	private boolean recoveryMode;
	private static Logger logger;

	static {
		Logging.loggingConfiguration();
		logger = LogManager.getLogger(AppManager.class);
	}

	/**
	 * @throws AutomaticTasksProcessingSystemException 
	 * 
	 */
	public AppManager() throws AutomaticTasksProcessingSystemException {
		logger.info("== Automatic Tasks Processing System is starting ==");

		try {
			initialization = new Initialization(this);
			starting = new Starting(this);
			recovery = new Recovery(this);
			processing = new Processing(this);
			stopping = new Stopping(this);
			recovery = new Recovery(this);
		} catch (AutomaticTasksProcessingSystemException exception) {
			logger.fatal("Cannot create state machine.");
			throw new AutomaticTasksProcessingSystemException("S-002", "Cannot create state machine.", exception.getCause());
		}

		state = initialization;
	}

	public void initialize() {
		state.initialize();
	}

	public void recover() {
		state.recover();
	}

	public void start() {
		state.start();
	}

	public void process() {
		state.process();
	}

	public void stop() {
		state.stop();
	}

	/**
	 * @return The recovery.
	 */
	public State getRecovery() {
		return recovery;
	}

	/**
	 * @param state The state to set.
	 */
	public void setState(State state) {
		this.state = state;
	}

	/**
	 * @return The initialization.
	 */
	public State getInitialization() {
		return initialization;
	}

	/**
	 * @return The starting.
	 */
	public State getStarting() {
		return starting;
	}

	/**
	 * @return The processing.
	 */
	public State getProcessing() {
		return processing;
	}

	/**
	 * @return The stopping.
	 */
	public State getStopping() {
		return stopping;
	}

	/**
	 * @return The recoveryMode.
	 */
	public boolean isRecoveryMode() {
		return recoveryMode;
	}

	/**
	 * @param recoveryMode The recoveryMode to set.
	 */
	public void setRecoveryMode(boolean recoveryMode) {
		this.recoveryMode = recoveryMode;
	}
}
